<?php

include APPPATH . '/vendor/autoload.php';

use OSS\OssClient;
use OSS\Core\OssException;


class Controller_Video extends Controller_Website {

    public function action_list() {

        $where = array();
        $where['status'] = Arr::get($_GET, 'status');
        $where['ORDER'] = 'id DESC';
        $where = array_filter($where, 'strlen');

        $m_video = Model::factory('video');
        $total = $m_video->count($where);
        $pager = new Pager($total, 10);
        $list = $m_video->select($pager->offset, $pager->size, $where)->as_array();
        
       
        $this->content = View::factory('video/list');
        $this->content->list = $list;
        $this->content->pager = $pager;


    }
    public function action_add() {

        if (!empty($_POST)) {
            $datas = $_POST;
//        print_r($data);exit;
//            $data = $this->_get_data($_POST);
//            print_r($data);exit;
            $data = [];
            $data['video_title'] = $datas['video_title'];
            $data['url'] = $datas['video'][0];
            $data['pic'] = $datas['pic'][0];
            $data['add_time'] = time();
            $data['edit_time'] = time();
            $m_article = Model::factory('video');

            $ret = $m_article->insert($data);
            if ($ret !== false) {
                $this->redirect('video/list');
            }
        }

        $this->content = View::factory('video/add');
        $this->content->info = null;


    }

    
    /*public function action_add2() {

        if (!empty($_POST)) {
//        print_r($_POST);;
            if (!empty($_FILES))  {
//                 var_dump($_FILES);
//                 exit;
                if($_FILES["video_name"]["name"])
                {
                    $filename=$_FILES["video_name"]["name"];
                    $filetype=substr(strrchr($filename,'.'),1);//这个变量存的就是扩展名
                    if($filetype != 'mp4' && $filetype != 'avi' && $filetype != 'mov'){
                        echo "<script language=javascript>alert('上传视频格式不符合,请重新上传！');history.back();</script>";
                        exit;
                    }
                }

                $filePath = Upload::save($_FILES['video_name'], time().'.'.$filetype, '/tmp');
                $accessKeyId = 'LTAIQbn684Xpjs5c';
                $accessKeySecret = 'HTcL8wEGjpgVgFX0H89sUhrtKxLLu9';
                $endpoint = 'http://oss-cn-hangzhou.aliyuncs.com';
                try {
                    $ff = time().'.'.$filetype;
                    $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
                } catch (OssException $e) {
                    print $e->getMessage();
                }
                $bucket = "image-redcells";
                $this->uploadFile($ossClient, $bucket, $filePath, $ff);
//                 exit;
            }

            $data = $this->_get_data($_POST);
            $data['url'] = 'http://jtkj.oss-cn-beijing.aliyuncs.com/'.$ff;
            $data['add_time'] = time();
            $m_article = Model::factory('video');
            $ret = $m_article->insert($data);
            if ($ret !== false) {
               $this->redirect('video/list');
            }
        }

        $this->content = View::factory('video/add');
        $this->content->info = null;


    }

    function uploadFile($ossClient, $bucket, $filePath, $ff)
    {
        if($_FILES["video_name"]["name"])
        {
            //                 var_dump($_FILES);exit;
            $filename=$_FILES["video_name"]["name"];
            $filetype=substr(strrchr($filename,'.'),1);//这个变量存的就是扩展名
            if($filetype != 'mp4' && $filetype != 'avi' && $filetype != 'mov'){
                echo "<script language=javascript>alert('上传视频格式不符合,请重新上传！');history.back();</script>";
                exit;
            }
        }
        $object = $ff;
        try{
            $ossClient->uploadFile($bucket, $object, $filePath);
        } catch(OssException $e) {
            printf(__FUNCTION__ . ": FAILED\n");
            printf($e->getMessage() . "\n");
            return;
        }
        print(__FUNCTION__ . ": OK" . "\n");
    }

    public function action_edit() {
        $id = Arr::get($_GET, 'id');
        $m_video = Model::factory('video');
        $info = $m_video->getRowById($id);
//         var_dump($info);exit;
        if (!empty($_POST)) {
//             var_dump($_POST);exit;
            if($_FILES["video_name"]["name"])
            {
//                 var_dump($_FILES);exit;
                $filename=$_FILES["video_name"]["name"];
                $filetype=substr(strrchr($filename,'.'),1);//这个变量存的就是扩展名
                if($filetype != 'mp4' && $filetype != 'avi' && $filetype != 'mov'){
                    echo "<script language=javascript>alert('上传视频格式不符合,请重新上传！');history.back();</script>";
                    exit;
                }
            }
            
            $filePath = Upload::save($_FILES['video_name'], time().'.'.$filetype, '/tmp');
            $accessKeyId = 'LTAIQbn684Xpjs5c';
            $accessKeySecret = 'HTcL8wEGjpgVgFX0H89sUhrtKxLLu9';
            $endpoint = 'http://oss-cn-hangzhou.aliyuncs.com';
            try {
                $ff = time().'.'.$filetype;
                $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
            } catch (OssException $e) {
                print $e->getMessage();
            }
            $bucket = "image-redcells";
            $this->uploadFile($ossClient, $bucket, $filePath, $ff);
            //                 exit;
        
            
            $data = $this->_get_data($_POST);
            $data['url'] = 'http://jtkj.oss-cn-beijing.aliyuncs.com/'.$ff;
            $ret = $m_video->updateById($data, $id);
            if ($ret !== false) {
                $this->redirect('video/list');
            }
        }
        
        $this->content = View::factory('video/edit');
        $this->content->info = $info;

    } */

    public function action_del() {
        $id = $_GET['id'];
        $m_video = Model::factory('video');
        $ret = $m_video->deleteById($id);
        if ($ret !== false) {
            $this->redirect(Request::$referrer);
        }
    }

    public function action_close() {
        $id = $_GET['id'];
        $data = array(
            'status' => '0',
        );
        $m_video = Model::factory('video');
        $ret = $m_video->updateById($data, $id);
        if ($ret !== false) {
            $this->redirect(Request::$referrer);
        }
    }
    
    public function action_open() {
        $id = $_GET['id'];
        $data = array(
            'status' => '1',
        );
        $m_video = Model::factory('video');
        $ret = $m_video->updateById($data, $id);
        if ($ret !== false) {
            $this->redirect(Request::$referrer);
        }
    }

    protected function _get_data($post) {
        $data = array_intersect_key($post, array_flip(array('video_title','desc','pic')));
        
        if (!empty($data['pic'])) {
            $data['pic'] = array_pop($data['pic']);
        }
        $data['edit_time'] = strtotime('now');
        return $data;
    }
    
}
